package com.platform.controller;

import com.platform.annotation.SysOptionLog;
import com.platform.entity.UserAssetEntity;
import com.platform.entity.excel.UserAssetExcel;
import com.platform.service.IncomeCoinService;
import com.platform.service.UserAssetService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员管理:用户资产
 */
@RestController
@RequestMapping("/userasset")
public class UserAssetController {
    @Autowired
    private UserAssetService userAssetService;

    @Autowired
    private IncomeCoinService incomeCoinService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("userasset:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserAssetEntity> tUserAssetList = userAssetService.queryList(query);
        int total = userAssetService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tUserAssetList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("userasset:info")
    public R info(@PathVariable("id") Integer id) {
        UserAssetEntity tUserAsset = userAssetService.queryObject(id);

        return R.ok().put("tUserAsset", tUserAsset);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("userasset:save")
    @SysOptionLog("添加用户资产")
    public R save_asset(@RequestBody UserAssetEntity tUserAsset) {
        userAssetService.save(tUserAsset);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("userasset:update")
    @SysOptionLog("修改用户资产")
    public R update_asset(@RequestBody UserAssetEntity tUserAsset) {
        //保存这次修改
        userAssetService.update(tUserAsset);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("userasset:delete")
   // @SysOptionLog("删除用户资产")
    public R delete_asset(@RequestBody Integer[] ids) {
        userAssetService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserAssetEntity> list = userAssetService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/export")
    @RequiresPermissions("userasset:export")
    public Object code(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "phone", required = false) String phone,
                       @RequestParam(value = "sort", required = false) Integer sort,
                       @RequestParam(value = "done", required = false) Integer done,
                       @RequestParam(value = "index",required = false) Integer index,
                       @RequestParam(value = "size",required = false) Integer size,
                       HttpServletResponse response) throws IOException {

        List<UserAssetExcel> datas = userAssetService.queryExcelList( name, phone, sort,done, index, size);
        if (datas.isEmpty()) {
            return R.error("导出数据错误");
        }
        ExcelExport ee = new ExcelExport("用户资产");
        String[] header = new String[]{"用户编号", "用户名", "手机号码", "总金额", "冻结金额", "链总数", "链冻结"};
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserAssetExcel excel : datas) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            map.put("uid", excel.getUid());
            map.put("userName", excel.getUserName());
            map.put("phone", excel.getPhone());
            map.put("amount", excel.getAmount());
            map.put("frozen", excel.getFrozen());
            map.put("coin", excel.getCoin());
            map.put("coinFrozen", excel.getCoinFrozen());
            map.put("D-one", excel.getDone()==1?"是":"否");
            list.add(map);
        }
        ee.addSheetByMap("用户资产", list, header);
        ee.export(response);
        return R.ok();

    }

}
